package com.baizhi.serviceimpl;

import com.baizhi.dao.UserMapper;
import com.baizhi.entity.User;
import com.baizhi.entity.UserExample;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        //设置当前页
        map.put("page",pageSize);

        //设置查询的条件对象
        UserExample example = new UserExample();
        //查询总条数
        int count = userMapper.selectCountByExample(example);

        //设置总条数
        map.put("total",count);

        /* (page-1)*pageSize */

        //创建分页对象    参数：起始条数，数据数
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<User> users = userMapper.selectByExampleAndRowBounds(example, rowBounds);

        //设置数据
        map.put("rows",users);

        return map;
    }

    @Override
    public HashMap<String, Object> update(User user) {

        HashMap<String, Object> map = new HashMap<>();

        try {
            userMapper.updateByPrimaryKeySelective(user);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }

        return map;
    }

    @Override
    public CommonVO updates(User user) {
        //HashMap<String, Object> map = new HashMap<>();

        /*CommonVO commonVO = new CommonVO();

        try {
            userMapper.updateByPrimaryKeySelective(user);
           // map.put("message","修改成功");
            commonVO.setMessage("修改成功");
            commonVO.setStatus(200);
        } catch (Exception e) {
            e.printStackTrace();
           // map.put("message","修改失败");
            commonVO.setMessage("修改失败");
            commonVO.setStatus(400);
        }*/

        try {
            userMapper.updateByPrimaryKeySelective(user);
            return CommonVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild();
        }
    }
    @Override
    public CommonQueryPageVO queryAllPages(Integer page, Integer pageSize) {

        //设置查询的条件对象
        UserExample example = new UserExample();
        //查询总条数
        int total = userMapper.selectCountByExample(example);

        //创建分页对象    参数：起始条数，数据数
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<User> users = userMapper.selectByExampleAndRowBounds(example, rowBounds);

        return new CommonQueryPageVO(page,total,users);
    }
}
